/*
 * Powered By XY
 * Since 2020
 */
package com.xy.biz.fd.manager.impl;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import com.xy.biz.fd.domain.consts.RepositoryConstant;
import com.xy.biz.fd.domain.enums.RepositoryTypeEnum;
import com.xy.biz.fd.domain.po.RepositoryPO;
import com.xy.tool.context.UserContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xy.biz.fd.domain.bo.RepositoryBO;
import com.xy.biz.fd.domain.query.RepositoryQuery;
import com.xy.biz.fd.domain.converter.RepositoryConverter;
import com.xy.biz.fd.manager.RepositoryManager;
import com.xy.biz.fd.dao.RepositoryDao;
import org.springframework.util.CollectionUtils;

/**
 *
 *
 * @author ssl
 * @date 2021-1-18
 */
@Service
public class RepositoryManagerImpl implements RepositoryManager {

    @Autowired
    private RepositoryDao repositoryDao;

    @Override
    public int saveRepository(RepositoryBO repositoryBO) {
        RepositoryPO repositoryPO = RepositoryConverter.toPO(repositoryBO);
        repositoryDao.saveRepository(repositoryPO);
        repositoryBO.setRepositoryId(repositoryPO.getRepositoryId());
        return repositoryBO.getRepositoryId().intValue();
    }

    @Override
    public int updateRepository(RepositoryBO repositoryBO) {
        return repositoryDao.updateRepository(RepositoryConverter.toPO(repositoryBO));
    }

    @Override
    public int deleteRepository(RepositoryBO repositoryBO) {
        return repositoryDao.deleteRepository(RepositoryConverter.toPO(repositoryBO));
    }

    /**
     * 批量删除
     *
     * @param repositoryIds
     */
    @Override
    public int deleteRepositoryByIds(Collection<Long> repositoryIds) {
        return repositoryDao.deleteRepositoryByIds(repositoryIds);
    }

    @Override
    public RepositoryBO getRepository(Long repositoryId) {
        return RepositoryConverter.fromPO(repositoryDao.getRepository(repositoryId));
    }

    @Override
    public List<RepositoryBO> listRepositoryById(Collection<Long> collection) {
        return RepositoryConverter.fromPO(repositoryDao.listRepositoryById(collection));
    }

    @Override
    public List<RepositoryBO> listRepository(RepositoryQuery query) {
        return RepositoryConverter.fromPO(repositoryDao.listRepository(query));
    }

    @Override
    public int countRepository(RepositoryQuery query) {
        return repositoryDao.countRepository(query);
    }

    @Override
    public List<RepositoryBO> listRepositoryByName(RepositoryQuery query) {
        return RepositoryConverter.fromPO(repositoryDao.listRepositoryByName(query));
    }

    @Override
    public List<RepositoryBO> listRubbishRepository(RepositoryQuery query) {
        return RepositoryConverter.fromPO(repositoryDao.getRubbishRepository(query));
    }

    @Override
    public List<RepositoryBO> getUserGroupAndRepository(Set<Long> collect) {
        return RepositoryConverter.fromPO(repositoryDao.getUserGroupAndRepository(collect));
    }

    /**
     * 通过用户id获取知识库
     * @param userId
     * @return
     */
    @Override
    public List<RepositoryBO> getRepositoryByUserId(Long userId) {
        return RepositoryConverter.fromPO(repositoryDao.getRepositoryByUserId(userId));
    }

    @Override
    public RepositoryBO getReleaseRepository() {
        RepositoryQuery query = RepositoryQuery.one().belongType(RepositoryConstant.BELONG_TYPE_RELEASE).belongTo(UserContextHolder.getUserId()).build();
        //判断发行知识库是否存在
        List<RepositoryBO> repositoryBOS = RepositoryConverter.fromPO(repositoryDao.getRubbishRepository(query));
        if (CollectionUtils.isEmpty(repositoryBOS)) {
            //新建发行知识库
            LocalDateTime now = LocalDateTime.now();
            RepositoryBO repositoryBO = new RepositoryBO();
            repositoryBO.setBelongType(RepositoryConstant.BELONG_TYPE_RELEASE);
            repositoryBO.setBelongTo(UserContextHolder.getUserId());
            repositoryBO.setMemo("初始化发行知识库");
            repositoryBO.setRepositoryName("发行知识库");
            repositoryBO.setCapacitySize(0L);
            repositoryBO.setType(RepositoryTypeEnum.CUSTOM.getCode());
            repositoryBO.setRepositorySize(0L);
            repositoryBO.setTenantId(UserContextHolder.getTenantId());
            repositoryBO.setAccessTime(now);
            repositoryBO.setUpdateTime(now);
            repositoryBO.setCreateTime(now);
            repositoryBO.setCreateUserId(UserContextHolder.getUserId());
            repositoryBO.setDeleteFlag(false);
            boolean flag = false;
            for (int i = 0; i < 3; i++) {
                if (saveRepository(repositoryBO) > 0) {
                    flag = true;
                    break;
                }
            }
            if (flag) {
                return repositoryBO;
            }
            return null;
        }
        return repositoryBOS.get(0);
    }

    /**
     * 根据归属Id查找多条
     *
     * @param repositoryBO 查找条件
     * @return
     */
    @Override
    public List<RepositoryBO> listRepositoryByBelongTo(RepositoryBO repositoryBO) {
        RepositoryPO repositoryPO = RepositoryConverter.toPO(repositoryBO);
        return RepositoryConverter.fromPO(repositoryDao.listRepositoryByBelongTo(repositoryPO));
    }

}
